namespace DotNetNuke.Services.Search
{
    using DotNetNuke.Entities.Modules;
    using DotNetNuke.Framework;
    using DotNetNuke.Services.Exceptions;
    
    using System;
    using System.Collections;
    using System.Runtime.CompilerServices;

    /// -----------------------------------------------------------------------------
    /// Namespace:  DotNetNuke.Services.Search
    /// Project:    DotNetNuke.Search.Index
    /// Class:      ModuleIndexer
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The ModuleIndexer is an implementation of the abstract IndexingProvider
    /// class
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    /// [cnurse]	11/15/2004	documented
    /// </history>
    /// -----------------------------------------------------------------------------
    public class ModuleIndexer : IndexingProvider
    {
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// GetModuleList gets a collection of SearchContentModuleInfo Items for the Portal
        /// </summary>
        /// <remarks>
        /// Parses the Modules of the Portal, determining whetehr they are searchable.
        /// </remarks>
        /// <param name="PortalID">The Id of the Portal</param>
        /// <history>
        /// [cnurse]	11/15/2004	documented
        /// </history>
        /// -----------------------------------------------------------------------------
        protected SearchContentModuleInfoCollection GetModuleList(int PortalID)
        {
            IEnumerator refObjectHelperL0=null;
            SearchContentModuleInfoCollection Results = new SearchContentModuleInfoCollection();
            ArrayList arrModules = new ModuleController().GetSearchModules(PortalID);
            Hashtable businessControllers = new Hashtable();
            Hashtable htModules = new Hashtable();
            try
            {
                refObjectHelperL0 = arrModules.GetEnumerator();
                while (refObjectHelperL0.MoveNext())
                {
                    ModuleInfo objModule = (ModuleInfo) refObjectHelperL0.Current;
                    if (!htModules.ContainsKey(objModule.ModuleID))
                    {
                        try
                        {
                            object objController = (businessControllers[objModule.BusinessControllerClass]);
                            if (objController == null)
                            {
                                objController = (Reflection.CreateObject(objModule.BusinessControllerClass, objModule.BusinessControllerClass));
                                businessControllers.Add(objModule.BusinessControllerClass, (objController));
                            }
                            if (objController is ISearchable)
                            {
                                SearchContentModuleInfo ContentInfo = new SearchContentModuleInfo();
                                ContentInfo.ModControllerType = (ISearchable) objController;
                                ContentInfo.ModInfo = objModule;
                                Results.Add(ContentInfo);
                            }
                        }
                        catch (Exception exception1)
                        {
            
                            Exception ex = exception1;
                            DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
            
                        }
                        finally
                        {
                            htModules.Add(objModule.ModuleID, objModule.ModuleID);
                        }
                    }
                }
            }
            finally
            {
                if (refObjectHelperL0 is IDisposable)
                {
                    (refObjectHelperL0 as IDisposable).Dispose();
                }
            }
            return Results;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// GetSearchIndexItems gets the SearchInfo Items for the Portal
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="PortalID">The Id of the Portal</param>
        /// <history>
        /// [cnurse]	11/15/2004	documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public override SearchItemInfoCollection GetSearchIndexItems(int PortalID)
        {
            IEnumerator refObjectHelperL0=null;
            SearchItemInfoCollection SearchItems = new SearchItemInfoCollection();
            SearchContentModuleInfoCollection SearchCollection = this.GetModuleList(PortalID);
            try
            {
                refObjectHelperL0 = SearchCollection.GetEnumerator();
                while (refObjectHelperL0.MoveNext())
                {
                    SearchContentModuleInfo ScModInfo = (SearchContentModuleInfo) refObjectHelperL0.Current;
                    try
                    {
                        SearchItemInfoCollection myCollection = ScModInfo.ModControllerType.GetSearchItems(ScModInfo.ModInfo);
                        if (myCollection != null)
                        {
                            SearchItems.AddRange(myCollection);
                        }
                    }
                    catch (Exception exception1)
                    {
        
                        Exception ex = exception1;
                        DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
        
                    }
                }
            }
            finally
            {
                if (refObjectHelperL0 is IDisposable)
                {
                    (refObjectHelperL0 as IDisposable).Dispose();
                }
            }
            return SearchItems;
        }
    }
}

